package cn.darkfire.web.Controller;

import cn.darkfire.web.POJO.LoginUser;
import cn.darkfire.web.POJO.Status;
import cn.darkfire.web.Service.UserService;
import cn.darkfire.web.Service.impl.UserServiceImpl;
import cn.darkfire.web.Utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "reg.do")
public class RegController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(req.getParameter("username").length()>=3&&req.getParameter("password").length()>=5){
            UserService userService = new UserServiceImpl();
            //采用md5加密
            Status st = userService.register(new LoginUser(req.getParameter("username"), DataUtils.getInstance().stringToMD5(req.getParameter("password")).toUpperCase()));
            resp.getOutputStream().print(st.getCode());
        }else{
            resp.getOutputStream().print(500);
        }
    }
}
